using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 游戏配置服务 - 管理游戏配置数据
/// </summary>
public class GameConfigService : ServiceBase
{
    private Dictionary<string, object> m_ConfigData = new Dictionary<string, object>();
    
    protected override void OnInitialize()
    {
        LoadDefaultConfig();
        Logger.Log("GameConfigService", "GameConfigService initialized");
    }
    
    /// <summary>
    /// 加载默认配置
    /// </summary>
    private void LoadDefaultConfig()
    {
        // 网格配置
        SetConfig("Grid.Rows", 5);
        SetConfig("Grid.Cols", 9);
        SetConfig("Grid.KeyMultiplier", 100);
        
        // 游戏配置
        SetConfig("Game.InitSunCount", 300);
        SetConfig("Game.MaxSunCount", 9999);
        
        // 实体配置
        SetConfig("Entity.DefaultHealth", 100);
        SetConfig("Entity.DefaultSpeed", 1.0f);
        SetConfig("Entity.DefaultDamage", 20);
        
        // 性能配置
        SetConfig("Performance.EnemySearchCacheTime", 0.1f);
        SetConfig("Performance.MaxEntities", 1000);
    }
    
    /// <summary>
    /// 设置配置值
    /// </summary>
    public void SetConfig(string key, object value)
    {
        m_ConfigData[key] = value;
    }
    
    /// <summary>
    /// 获取配置值
    /// </summary>
    public T GetConfig<T>(string key, T defaultValue = default(T))
    {
        if (m_ConfigData.TryGetValue(key, out var value))
        {
            if (value is T)
            {
                return (T)value;
            }
        }
        return defaultValue;
    }
    
    /// <summary>
    /// 获取网格行数
    /// </summary>
    public int GetGridRows()
    {
        return GetConfig("Grid.Rows", 5);
    }
    
    /// <summary>
    /// 获取网格列数
    /// </summary>
    public int GetGridCols()
    {
        return GetConfig("Grid.Cols", 9);
    }
    
    /// <summary>
    /// 获取初始阳光数量
    /// </summary>
    public int GetInitSunCount()
    {
        return GetConfig("Game.InitSunCount", 300);
    }
    
    /// <summary>
    /// 获取敌人搜索缓存时间
    /// </summary>
    public float GetEnemySearchCacheTime()
    {
        return GetConfig("Performance.EnemySearchCacheTime", 0.1f);
    }
    
    /// <summary>
    /// 检查配置是否存在
    /// </summary>
    public bool HasConfig(string key)
    {
        return m_ConfigData.ContainsKey(key);
    }
    
    /// <summary>
    /// 获取所有配置键
    /// </summary>
    public List<string> GetAllConfigKeys()
    {
        return new List<string>(m_ConfigData.Keys);
    }
    
    protected override void OnDispose()
    {
        m_ConfigData.Clear();
        Logger.Log("GameConfigService", "GameConfigService disposed");
    }
}
